package br.com.colegio.dao.storage;

import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import br.com.colegio.dao.AlunoDAO;
import br.com.colegio.util.SpecialCharFilter;
import br.com.colegio.vo.Aluno;

public class AlunoData
{
	private static AlunoData instance;
	private Map<String, Aluno> data = new TreeMap<String, Aluno>(); 

	private AlunoData()
	{
	}

	public static AlunoData getInstance()
	{
		if (instance == null)
			instance = new AlunoData();

		return instance;
	}

	private Map<String, Aluno> getData()
	{
		if (data.size() == 0)
		{
			Collection<Object> r = AlunoDAO.list();

			if (r != null)
				for (Object o : r)
				{
					Aluno a = (Aluno)o;
					data.put(a.getCodigo(), a);
				}
		}

		return data;
	}

	public Aluno getAluno(String cod)
	{
		return getData().get(cod);
	}

	public void atualizaData(Aluno a)
	{
		getData().put(a.getCodigo(), a);
	}

	public void atualizaData(Set<Aluno> sa)
	{
		for (Aluno a : sa)
			getData().put(a.getCodigo(), a);
	}

	public Collection<Aluno> getAlunos()
	{
		Map<String, Aluno> ma = new TreeMap<String, Aluno>();

		for (String k : getData().keySet())
			ma.put(SpecialCharFilter.replaceSpecial(getData().get(k).getNome().toUpperCase()), getData().get(k));

		return ma.values();
	}

	public Collection<Aluno> getAlunos(String s)
	{
		Map<String, Aluno> ma = new TreeMap<String, Aluno>();

		s = SpecialCharFilter.replaceSpecial(s).toUpperCase();

		for (String k : getData().keySet())
			if (SpecialCharFilter.replaceSpecial(getData().get(k).getNome()).toUpperCase().contains(s))
				ma.put(SpecialCharFilter.replaceSpecial(getData().get(k).getNome().toUpperCase()), getData().get(k));

		return ma.values();
	}

	public Collection<Aluno> getAlunosStartsWith(String s)
	{
		Map<String, Aluno> ma = new TreeMap<String, Aluno>();

		s = SpecialCharFilter.replaceSpecial(s).toUpperCase();

		for (String k : getData().keySet())
			if (SpecialCharFilter.replaceSpecial(getData().get(k).getNome()).toUpperCase().startsWith(s))
				ma.put(SpecialCharFilter.replaceSpecial(getData().get(k).getNome().toUpperCase()), getData().get(k));

		return ma.values();
	}
}